package cc.xpcas.raft.message;

import java.util.function.*;

/**
 * @author xp
 */
public class MessageContext {

    private final Consumer<Message> replyHandler;

    private final Message message;

    public MessageContext(Consumer<Message> replyHandler, Message message) {
        this.replyHandler = replyHandler;
        this.message = message;
    }

    @SuppressWarnings("unchecked")
    public <T extends Message> T getMessage() {
        return (T) message;
    }

    public boolean reply(Message message) {
        if (replyHandler == null) return false;

        replyHandler.accept(message);
        return true;
    }
}
